package com.sixmac.platform.service.impl;

import com.sixmac.common.model.system.Role;
import com.sixmac.common.model.system.User;
import com.sixmac.core.base.service.BaseService;
import com.sixmac.core.plugins.dao.Blade;
import com.sixmac.core.shiro.ShiroKit;
import com.sixmac.core.toolbox.ajax.AjaxResult;
import com.sixmac.core.toolbox.cache.CacheKit;
import com.sixmac.module.Installer;
import com.sixmac.module.Landlord;
import com.sixmac.module.Room;
import com.sixmac.platform.service.IndexService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * Created by admin on 2018/9/5.
 */

@Service
public class IndexServiceImpl extends BaseService<Installer> implements IndexService {

    String SYS_CACHE = "SYS_CACHE";//系统变量 10小时

    public AjaxResult recordRoom(Installer installer,String lng, String lat, Integer region, String address, String images, String mobile, String lockNo,String roomNum) {
        Landlord landlord = Blade.create(Landlord.class).findFirstBy("mobile=#{mobile}", getData("mobile", mobile));

        Room r = Blade.create(Room.class).findFirstBy("lockNo=#{lockNo}", getData("lockNo", lockNo));

        //首先通过手机号码查找房东，如果房东不存在，则提示房东不存在
        if (landlord == null)
        {
            landlord = new Landlord();
            landlord.setMobile(mobile);
            landlord.setCreateDate(new Date());
            Blade.create(Landlord.class).saveRtId(landlord);

            User user = Blade.create(User.class).findFirstBy(" account ='"+mobile+"'",null);
            if(user==null){
                user = new User();
                Role role =Blade.create(Role.class).findFirstBy(" tips='landlord'",null);
                String salt = ShiroKit.getRandomSalt(5);
                String pwdMd5 = ShiroKit.md5("123456", salt);
                user.setPassword(pwdMd5);
                user.setSalt(salt);
                user.setStatus(1);
                user.setName(mobile);
                user.setRoleid(role.getId()+"");
                user.setDeptid(1);
                user.setAccount(mobile);
                user.setCreatetime(new Date());
                user.setPhone(mobile);
                boolean temp = Blade.create(User.class).save(user);
                if (temp) {
                    CacheKit.removeAll(SYS_CACHE);
                }
            }
        }
        landlord = Blade.create(Landlord.class).findFirstBy("mobile=#{mobile}", getData("mobile", mobile));

        Room room = new Room();
        if (r!=null){
            room.setId(r.getId());
        }
        room.setCreateDate(new Date());
        room.setAddress(address);
        room.setLng(lng);
        room.setLat(lat);
        room.setRoomNo(roomNum);
        room.setRegion(region);
        room.setImages(images);
        room.setLockNo(lockNo);
        room.setElectric(new BigDecimal(100));
        room.setLandlordId(landlord.getId());
        Blade.create(Room.class).saveOrUpdateEveryCol(room);
        return success("绑定成功");
    }
}
